﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;

namespace CamDo.DTO
{
    class Config
    {
        private string server;
        private string database;
        private string username;
        private string password;

        public string Server
        {
            get { return server; }
            set { server = value; }
        }

        public string Database
        {
            get { return database; }
            set { database = value; }
        }
        
        public string Username
        {
            get { return username; }
            set { username = value; }
        }
        
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        public Config()
        {
            StreamReader reader=new StreamReader("ConnectString.con");
            this.Server = reader.ReadLine().Split(':')[1];
            this.Database = reader.ReadLine().Split(':')[1];
            this.Username = reader.ReadLine().Split(':')[1];
            this.Password = reader.ReadLine().Split(':')[1];
            reader.Close();
        }

        public SqlConnection GetConnect()
        {
            if (this.Username !="")
                return new SqlConnection("Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";User Id=" + this.Username + ";Password=" + this.Password + ";");
            else
                return new SqlConnection("Data Source=" + this.Server + ";Initial Catalog=" + this.Database + ";Integrated Security=True");
        }

        public static void WriteFile(string server,string data,string uid,string pass)
        {
            StreamWriter writer = new StreamWriter("ConnectString.con");
            writer.WriteLine("Server:"+server);
            writer.WriteLine("Database:"+data);
            writer.WriteLine("UserName:" + uid);
            writer.WriteLine("PassWord:" + pass);
            writer.Close();
        }
    }
}
